package com.example.finalwork.controller;


import com.example.finalwork.entity.ResponseMessage;
import com.example.finalwork.entity.User;
import com.example.finalwork.entity.dao.UserDao;
import com.example.finalwork.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController                       //接口返回方法对象，默认返回为json文本
//允许跨域请求的前端地址http://localhost:3001   @CrossOrigin(origins = "*")
@RequestMapping("/users")           //localhost:8080/users
public class UserController {

    @Autowired
    IUserService userService;
    @PostMapping("/login")
    public ResponseMessage validateLogin(@RequestBody UserDao loginRequest) {
        try {
            // 根据邮箱和密码验证用户
            User user = userService.getByEmailAndPassword(loginRequest.getEmail(), loginRequest.getPassword());

            if (user != null) {
                return ResponseMessage.success(user); // 成功返回用户信息
            } else {
                return ResponseMessage.error("邮箱或密码错误"); // 错误提示
            }
        } catch (IllegalArgumentException e) {
            // 捕获已知错误
            return ResponseMessage.error(e.getMessage());
        } catch (Exception e) {
            // 捕获未知错误
            e.printStackTrace();
            return ResponseMessage.error("登录失败，请稍后重试");
        }

    }

    //增加
    @PostMapping("/add")
    public ResponseMessage addUser(@Validated @RequestBody UserDao user) {
        try {
            User userAdd = userService.add(user);
            return ResponseMessage.success(userAdd);
        } catch (IllegalArgumentException e) {
            // 捕获用户已存在的错误，返回相应的错误信息
            return ResponseMessage.error(e.getMessage());
        }
    }

    //删除
    @DeleteMapping("/delete/{userId}")
    public ResponseMessage deleteUser(@PathVariable Integer userId) {
        userService.delete(userId);
        return ResponseMessage.success();
    }
    //修改
    @PutMapping("/update")
    public ResponseMessage editUser(@Validated @RequestBody UserDao user) {
        User userAdd=userService.edit(user);
        return ResponseMessage.success(userAdd);
    }
    //查询
    @GetMapping("/{userId}")
    public ResponseMessage getUser(@PathVariable Integer userId) {
        userService.get(userId);
        return ResponseMessage.success();

    }
    // 按年龄查询用户
    // 查询年龄范围内的用户
    @GetMapping("/ageRange")
    public ResponseMessage getUsersByAgeRange(
            @RequestParam(required = false) Integer minAge,
            @RequestParam(required = false) Integer maxAge) {
        List<User> users = userService.getUsersInAgeRange(minAge, maxAge);
        return ResponseMessage.success(users);
    }
    //按用户名查找
    @GetMapping("/userName/{userName}")
    public ResponseMessage getUserByUserName(@PathVariable String userName) {
        User user = userService.getByName(userName);
        return ResponseMessage.success(user);
    }

    @GetMapping("/search/nickname")
    public ResponseMessage searchByNickname(@RequestParam String nickname) {
        List<User> users = userService.getUsersByNickname(nickname);
        return ResponseMessage.success(users);
    }

}
